import request from '@/utils/request';

export function login(data) {
  return request({
    url: '/api/auth/login',
    method: 'post',
    data,
    responseType: 'json',
    headers: {
      'Accept': 'application/json'
    }
  }).then(response => {
    if (response && response.tokenValue) {
      localStorage.setItem('satoken', response.tokenValue);
      
      if (response.tokenName) {
        localStorage.setItem('tokenName', response.tokenName);
      }
      
      if (response.user) {
        localStorage.setItem('userInfo', JSON.stringify(response.user));
      }
    }
    return response;
  });
}

export function register(data) {
  return request({
    url: '/api/auth/register',
    method: 'post',
    data
  });
}

export function logout() {
  return request({
    url: '/api/auth/logout',
    method: 'post'
  });
}

export function getUserInfo() {
  return request({
    url: '/api/user/info',
    method: 'get'
  }).then(response => {
    if (response) {
      localStorage.setItem('userInfo', JSON.stringify(response));
    }
    return response;
  });
}

export function updateUserInfo(data) {
  return request({
    url: '/api/user/info',
    method: 'put',
    data
  });
} 